package com.wac.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wac.data.entity.*;
import com.wac.data.mapper.*;
import com.wac.data.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wac.data.utils.DataListUtil;
import com.wac.data.utils.UpLoad;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author beta
 * @since 2023-03-16
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

	private Map<String, Object> map;
	private Page<Article> thePage;

	@Resource
	private ArticleMapper articleMapper;
	@Resource
	private CommentMapper commentMapper;
	@Resource
	private UserService userService;
	@Resource
	private StateService stateService;
	@Resource
	private CommentService commentService;
	@Resource
	private LikeService likeService;

	//插入文章
	@Override
	public boolean addArticle(Article article, List<MultipartFile> file) {
		/**
		 * 腾讯版云存版 下面是本地版
		 */
		// if (file != null) {
		// 	String url = new TencentCouldCos().uploadfile(file, 3);
		// 	if (url == null) {
		// 		System.out.println("图片上传问题");
		// 		return false;
		// 	}
		// 	article.setCover(url);
		// }
		if (file.size() == 1) {
			String url = new UpLoad().upLoadFiles(file, 3).get("url").toString();
			if (url == null) {
				return false;
			}
			article.setCover(url);
			if (articleMapper.insert(article) == 1) {
				return true;
			}
		}
		return false;
	}

	@Override
	public Map<String, Object> getArticle(int page) {
		thePage = new Page<>(page, 5);
		articleMapper.selectPage(thePage, new QueryWrapper<Article>().orderByDesc("time"));
		//子查询顺便封装
		thePage.getRecords().forEach(article -> {
			//计算该文章下的评论数
			article.setCommentNum(commentMapper.selectCount(new QueryWrapper<Comment>().eq("comment_id", article.getId())).intValue());
		});
		return new DataListUtil().data(thePage);
	}

	@Override
	public Article getArticleById(String id) {
		//获取文章原始数据
		Article article = articleMapper.selectById(id);
		//赋值到文章实体
		article.setUser(userService.getUserSimpleInfo(article.getUserId()));
		//查询其他信息
		article.setCommentNum(commentMapper.selectCount(new QueryWrapper<Comment>().eq("comment_id", article.getId())).intValue());
		stateService.addView(id, 1);
		return article;
	}

	@Override
	public Map<String, Object> search(String text, int page) {
		thePage = new Page<>(page, 10);
		articleMapper.selectPage(thePage, new QueryWrapper<Article>().like("title", text).or().like("content", text).or().eq("title", text).or().orderByAsc("title"));
		thePage.getRecords().forEach(article -> {
			//赋值到文章实体
			article.setUser(userService.getUserSimpleInfo(article.getUserId()));
			//查询其他信息
			article.setCommentNum(commentMapper.selectCount(new QueryWrapper<Comment>().eq("comment_id", article.getId())).intValue());
		});
		return new DataListUtil().data(thePage);
	}

	@Override
	public boolean delete(String id) {
		String url = getArticleById(id).getCover();
		if (new UpLoad().deleteFile(url)) {
			commentMapper.selectList(new QueryWrapper<Comment>().eq("comment_id", id)).forEach(comment -> {
				commentService.delete(comment.getId());
			});
			likeService.delete(id);
			if (articleMapper.deleteById(id) == 1) {
				return true;
			}
		}
		return false;
	}
}
